//
//  ZYChatCell.m
//  Tourism
//
//  Created by 张亚 on 16/1/25.
//  Copyright © 2016年 zhangya. All rights reserved.
//

#import "ZYChatCell.h"
#import "ZYChatFrame.h"
#import "ZYChatMode.h"
#import "UIImage+Extension.h"


#define ZYNameFont [UIFont systemFontOfSize:15]

@interface ZYChatCell ()
@property (nonatomic,weak) UILabel * timeView;

@property (nonatomic,weak) UIImageView * iconView;

@property (nonatomic,weak) UIButton * textView;

@end

@implementation ZYChatCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString * cellID = @"cell";
    ZYChatCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[ZYChatCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}
//初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UILabel * timeView = [[UILabel alloc]init];
        timeView.textAlignment = NSTextAlignmentCenter;
        timeView.textColor = [UIColor grayColor];
        [self.contentView addSubview:timeView];
        self.timeView = timeView;
        
        UIImageView * iconView = [[UIImageView alloc]init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        
        UIButton * textView = [[UIButton alloc]init];
        [self.contentView addSubview:textView];
//        textView.backgroundColor = [UIColor greenColor];
        textView.titleLabel.numberOfLines = 0;
        
        [textView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//注意是textView 而不是self.textView
        textView.contentEdgeInsets = UIEdgeInsetsMake(ZYTextPadding, ZYTextPadding, ZYTextPadding, ZYTextPadding);
        textView.titleLabel.font = ZYNameFont;
        self.textView = textView;
        
        
        self.backgroundColor = [UIColor clearColor];
        
        
    }
    return self;
}


#pragma mark ---- 设置尺寸
- (void)setChatFrame:(ZYChatFrame *)chatFrame{
    _chatFrame = chatFrame;
    
    ZYChatMode * mode = chatFrame.chatMode;
    self.timeView.text = mode.time;
    self.timeView.frame = chatFrame.timeF;
    
    //头像
    NSString * icon = (mode.type == ZYChatModeTypeMe) ? @"me" : @"other";
    self.iconView.image = [UIImage imageNamed:icon];
    self.iconView.frame = chatFrame.iconF;
    
    //正文内容
    [self.textView setTitle:mode.text forState:UIControlStateNormal];
    self.textView.frame = chatFrame.textF;
    
    //正文的背景图片（给定的图片）
    if (mode.type == ZYChatModeTypeMe) {
        [self.textView setBackgroundImage:[UIImage resizableImage:@"chat_send_nor"] forState:UIControlStateNormal];
    }else{
        
        [self.textView setBackgroundImage:[UIImage resizableImage:@"chat_recive_nor"] forState:UIControlStateNormal];
        
    }
    
}
//    //封装一个图片拉伸的功能（返回一个随意拉伸不会改变的图片）
//- (UIImage *)resizableImage:(NSString *)name{
//    UIImage * normal = [UIImage imageNamed:name];
//    CGFloat w = normal.size.width * 0.5;
//    CGFloat h = normal.size.height * 0.5;
//    //这是将图片拉伸的一种模式（抽出一个小像素 进行平铺）(也有其他API)
//    UIImage * lastNormal = [normal resizableImageWithCapInsets:UIEdgeInsetsMake(w, h, w, h)];
//    return lastNormal;
//}




@end
